package com.club.football.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.club.football.common.UserHolder;
import com.club.football.dto.EventsSaveDto;
import com.club.football.dto.PageDto;
import com.club.football.entity.Events;
import com.club.football.entity.EventsTeams;
import com.club.football.entity.Team;
import com.club.football.mapper.EventsMapper;
import com.club.football.req.EventsPageReq;
import com.club.football.service.EventsService;
import com.club.football.service.EventsTeamsService;
import com.club.football.service.IntegralService;
import com.club.football.service.TeamService;
import com.club.football.utils.BeanUtils;
import com.club.football.utils.R;
import com.club.football.vo.EventsPageVo;
import com.club.football.vo.EventsVo;
import com.club.football.vo.IntegralVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.club.football.common.CommonConstants.*;

/**
 * 匹配服务实施
 *
 * @author 1
 * @date 2024/01/09
 */
@Service
public class EventsServiceImpl extends ServiceImpl<EventsMapper, Events> implements EventsService {
    @Resource
    private EventsTeamsService eventsTeamsService;

    @Resource
    private TeamService teamService;

    @Resource
    private IntegralService integralService;


    @Override
    public R<String> eventsSave(EventsSaveDto saveDto) {
        Events events = BeanUtils.copier(saveDto, new Events());
        Long userId = UserHolder.getUser().getUserId();
        events.setUpdateBy(userId);
        List<Long> teamIdList = saveDto.getTeamIdList();
        Long eventsId = events.getEventsId();
        if (eventsId != null) {
            List<EventsTeams> eventsTeamsList = teamIdList.stream().map(e -> new EventsTeams(eventsId, e)).collect(Collectors.toList());
            eventsTeamsService.lambdaUpdate().eq(EventsTeams::getEventsId, eventsId).remove();
            eventsTeamsService.saveBatch(eventsTeamsList);
            updateById(events);
            return R.ok(EDIT_OK_MSG);
        }
        long id = IdWorker.getId();
        List<EventsTeams> eventsTeamsList = teamIdList.stream().map(e -> new EventsTeams(id, e)).collect(Collectors.toList());
        events.setEventsId(id);
        events.setCreateBy(userId);
        baseMapper.insert(events);
        eventsTeamsService.saveBatch(eventsTeamsList);
        return R.ok(SAVE_OK_MSG);
    }

    @Override
    public R<PageDto<EventsPageVo>> eventsPage(PageDto<Events> pageDto, EventsPageReq eventsPageReq) {
        lambdaQuery().like(StringUtils.isNotBlank(eventsPageReq.getName()), Events::getName, eventsPageReq.getName())
                .page(pageDto);
        PageDto<EventsPageVo> eventsPage = new PageDto<>();
        eventsPage.setTotal(pageDto.getTotal());
        List<Events> records = pageDto.getRecords();
        if (records.isEmpty()) {
            return R.ok(eventsPage, GET_INFO_OK);
        }
        List<EventsPageVo> collect = records.stream().map(events -> {
            EventsPageVo eventsPageVo = BeanUtils.copier(events, new EventsPageVo());
            List<IntegralVo> list = integralService.list(events.getEventsId());
            eventsPageVo.setIntegralVoList(list);
            return eventsPageVo;
        }).collect(Collectors.toList());
        eventsPage.setRecords(collect);
        return R.ok(eventsPage, GET_INFO_OK);
    }

    @Override
    public R<EventsVo> eventsInfo(Long id) {
        Events events = getById(id);
        EventsVo eventsVo = BeanUtils.copier(events, new EventsVo());
        List<EventsTeams> eventsTeamsList = eventsTeamsService.lambdaQuery().eq(EventsTeams::getEventsId, id).list();
        List<Long> teamIdList = eventsTeamsList.stream().map(EventsTeams::getTeamId).collect(Collectors.toList());
        List<Team> teamsList = new ArrayList<>();
        if (CollUtil.isNotEmpty(teamIdList)) {
            teamsList = teamService.lambdaQuery().in(Team::getTeamId, teamIdList).list();
        }
        eventsVo.setTeamList(teamsList);
        eventsVo.setTeamIdList(teamIdList);
        return R.ok(eventsVo, GET_INFO_OK);
    }
}
